#include <iostream>
#include <string>

// ==================== 演示多重继承构造函数问题 ====================

class Base {
public:
    Base() {
        std::cout << "Base::Base()" << std::endl;
        value = 0;
    }
    
    Base(int v) {
        std::cout << "Base::Base(int): " << v << std::endl;
        value = v;
    }
    
    Base(const std::string& s) {
        std::cout << "Base::Base(string): " << s << std::endl;
        value = s.length();
    }

protected:
    int value;
};

// Mixin类 - 没有默认构造函数
class Mixin {
public:
    Mixin(double d) : data(d) {
        std::cout << "Mixin constructor: " << d << std::endl;
    }
    
    double getData() const { return data; }
    
private:
    double data;
};

// ==================== 问题演示：继承构造函数被删除 ====================
class ProblematicMultiDerived : public Base, public Mixin {
public:
    using Base::Base;  // 这会导致构造函数被隐式删除！
    
    // 编译器无法生成这些构造函数，因为不知道如何初始化Mixin：
    // ProblematicMultiDerived(int) : Base(int), Mixin(???) { } // Mixin需要double参数
    // ProblematicMultiDerived(string) : Base(string), Mixin(???) { } // 同样问题
};

// ==================== 解决方案1：提供默认值的Mixin ====================
class MixinWithDefault {
public:
    MixinWithDefault(double d = 1.0) : data(d) {
        std::cout << "MixinWithDefault constructor: " << d << std::endl;
    }
    
    double getData() const { return data; }
    
private:
    double data;
};

class WorkingMultiDerived1 : public Base, public MixinWithDefault {
public:
    using Base::Base;  // 现在可以工作了！MixinWithDefault有默认构造函数
    
    void show() const {
        std::cout << "WorkingMultiDerived1: Base value=" << value 
                  << ", Mixin data=" << getData() << std::endl;
    }
};

// ==================== 解决方案2：手动提供构造函数 ====================
class WorkingMultiDerived2 : public Base, public Mixin {
public:
    // 不使用using Base::Base，而是手动提供需要的构造函数
    WorkingMultiDerived2() : Base(), Mixin(0.0) {
        std::cout << "WorkingMultiDerived2 default constructor" << std::endl;
    }
    
    WorkingMultiDerived2(int v, double mixin_val = 1.0) : Base(v), Mixin(mixin_val) {
        std::cout << "WorkingMultiDerived2(int, double)" << std::endl;
    }
    
    WorkingMultiDerived2(const std::string& s, double mixin_val = 2.0) : Base(s), Mixin(mixin_val) {
        std::cout << "WorkingMultiDerived2(string, double)" << std::endl;
    }
    
    void show() const {
        std::cout << "WorkingMultiDerived2: Base value=" << value 
                  << ", Mixin data=" << getData() << std::endl;
    }
};

// ==================== 解决方案3：使用委托构造函数 ====================
class WorkingMultiDerived3 : public Base, public Mixin {
private:
    // 私有的通用构造函数
    template<typename... Args>
    WorkingMultiDerived3(double mixin_val, std::true_type, Args&&... args) 
        : Base(std::forward<Args>(args)...), Mixin(mixin_val) {
        std::cout << "WorkingMultiDerived3 internal constructor" << std::endl;
    }

public:
    // 公有的接口构造函数，使用默认mixin值
    WorkingMultiDerived3() : WorkingMultiDerived3(3.0, std::true_type{}) {}
    
    WorkingMultiDerived3(int v) : WorkingMultiDerived3(3.1, std::true_type{}, v) {}
    
    WorkingMultiDerived3(const std::string& s) : WorkingMultiDerived3(3.2, std::true_type{}, s) {}
    
    // 允许自定义mixin值的构造函数
    template<typename... Args>
    WorkingMultiDerived3(double mixin_val, Args&&... args) 
        : WorkingMultiDerived3(mixin_val, std::true_type{}, std::forward<Args>(args)...) {}
    
    void show() const {
        std::cout << "WorkingMultiDerived3: Base value=" << value 
                  << ", Mixin data=" << getData() << std::endl;
    }
};

// ==================== 测试函数 ====================
void test_problematic_case() {
    std::cout << "\n=== 问题演示 ===" << std::endl;
    
    // 以下代码会编译错误：
    // ProblematicMultiDerived pmd1(42);        // 错误：构造函数被隐式删除
    // ProblematicMultiDerived pmd2("hello");   // 错误：构造函数被隐式删除
    
    std::cout << "ProblematicMultiDerived的构造函数被隐式删除，无法编译" << std::endl;
}

void test_working_solutions() {
    std::cout << "\n=== 解决方案1：带默认参数的Mixin ===" << std::endl;
    
    WorkingMultiDerived1 wmd1_1;              // Base() + MixinWithDefault(1.0)
    WorkingMultiDerived1 wmd1_2(42);          // Base(42) + MixinWithDefault(1.0)
    WorkingMultiDerived1 wmd1_3("hello");     // Base("hello") + MixinWithDefault(1.0)
    
    wmd1_1.show();
    wmd1_2.show();
    wmd1_3.show();
    
    std::cout << "\n=== 解决方案2：手动构造函数 ===" << std::endl;
    
    WorkingMultiDerived2 wmd2_1;                    // 使用默认mixin值
    WorkingMultiDerived2 wmd2_2(42, 5.5);          // 自定义mixin值
    WorkingMultiDerived2 wmd2_3("world", 7.7);     // 自定义mixin值
    
    wmd2_1.show();
    wmd2_2.show();
    wmd2_3.show();
    
    std::cout << "\n=== 解决方案3：委托构造函数 ===" << std::endl;
    
    WorkingMultiDerived3 wmd3_1;                    // 使用默认mixin值
    WorkingMultiDerived3 wmd3_2(99);               // 使用默认mixin值
    WorkingMultiDerived3 wmd3_3("test");           // 使用默认mixin值
    WorkingMultiDerived3 wmd3_4(9.9, 123);        // 自定义mixin值
    
    wmd3_1.show();
    wmd3_2.show();
    wmd3_3.show();
    wmd3_4.show();
}

int main() {
    test_problematic_case();
    test_working_solutions();
    
    return 0;
}
